//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: hello@weaviate.io
//

package vectorizer

import (
	"context"
	"testing"

	"github.com/weaviate/weaviate/entities/moduletools"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/weaviate/weaviate/entities/models"
)

const image = "iVBORw0KGgoAAAANSUhEUgAAAGAAAAA/CAYAAAAfQM0aAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoTWFjaW50b3NoKSIgeG1wT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HffCJK0uA+wAfAtZYjIO+Gsi3TfOJK0sAfFP/jpKV+HBtKfkutOTPJ64sAVYD3qXgrmwpxVht6McnrmwBMAP4pjlYdRij3tCHT1xZAuDdermOA836gDKKqWNirob1ASZc2eeAl3QH36A+AGP+ohFWxNVSfYAuV9YKyKUTo/bgo2nUB5RQbImJuFqsD9DhyhbAuDgjMI36gFKX7S3XB5S6egSV2Bh8zYyDYjr4SGYi2yzmMIm5YnFGkFOLSQGNjY3X/BtaLBabWQF5XKcO6gOkZT950gAW6wPWuXoEZXEaOqoPyHLcPqkIwvqALFcCZHJmvqP6gEzH7VOKIKgPyHQlwIVUjRzWB1xw3H4+ubIFGE3VyGF9wKjj9ik3D4L6gFFXArCSTlEEzKe3LMIfwvYDNgcf+4P9csSVLUAXt7GD+oBuYfsuW4OvUR/Q7UoA/G2zaRvbOqEI0xRbYiKulusDTrgSYEg6sxKJIKwP6FLyjDYRV4v1ATpc2QKgNZtu6zTqA5o1ObM/h5eDyMvCtrlZObLgNhRv+jAHvkwqQjDzhYPfrvRvF0VcLdQHaHGNxWKrZv0d//hahcqr8Ccww1kRbwPuVMIXHRqd+ptimZiIq0F9gA2urEcQ2jkVf/tz0WG8ixTjnKEfn7iyBQi2WnuULLlV0qE9FrdzPnFlC4CGRQkvqyQ/MqRh6KtO2S948IkrWwC0XwHPAQ4r85z7w+TL1U8Y+8Q14S4oyjA9703AZ4AqFX8RvoTpN8i3/Bi/p+egHz5xZQsQGCasvqGuZhzj76DdpuIZx8FPuOAviWDG8e8qXl0yXxnHPnGdsf8FGAByGwC02iMZswAAAABJRU5ErkJggg=="

func TestVectorizer(t *testing.T) {
	t.Run("should vectorize image", func(t *testing.T) {
		// given
		client := &fakeClient{}
		vectorizer := New(client)
		config := newConfigBuilder().addSetting("imageFields", []interface{}{"image"}).build()

		props := map[string]interface{}{
			"image": image,
		}
		object := &models.Object{
			ID:         "some-uuid",
			Properties: props,
		}

		// when
		vector, _, err := vectorizer.Object(context.Background(), object, config)

		// then
		require.Nil(t, err)
		assert.NotNil(t, vector)
	})

	t.Run("should vectorize 2 image fields", func(t *testing.T) {
		// given
		client := &fakeClient{}
		vectorizer := New(client)
		config := newConfigBuilder().addSetting("imageFields", []interface{}{"image1", "image2"}).build()

		props := map[string]interface{}{
			"image1": image,
			"image2": image,
		}
		object := &models.Object{
			ID:         "some-uuid",
			Properties: props,
		}

		// when
		vector, _, err := vectorizer.Object(context.Background(), object, config)

		// then
		require.Nil(t, err)
		assert.NotNil(t, vector)
	})
}

func TestVectorizerWithWeights(t *testing.T) {
	client := &fakeClient{}
	vectorizer := New(client)
	config := newConfigBuilder().
		addSetting("imageFields", []interface{}{"image"}).
		addSetting("textFields", []interface{}{"text"}).
		addWeights([]interface{}{0.4}, []interface{}{0.6}).
		build()

	props := map[string]interface{}{
		"image":       image,
		"text":        "text",
		"description": "non-vectorizable",
	}
	input := &models.Object{
		ID:         "some-uuid",
		Properties: props,
	}

	vector, _, err := vectorizer.Object(context.Background(), input, config)

	require.Nil(t, err)
	assert.Equal(t, []float32{3.2, 6.4, 9.6, 12.8, 16}, vector)
	// vectors are defined in Vectorize within fakes_for_test.go
	// result calculated with above weights as (textVectors[0][i]*0.4+imageVectors[0][i]*0.6) / 2
}

func TestVectorizer_normalizeWeights(t *testing.T) {
	tests := []struct {
		name    string
		weights []float32
	}{
		{
			name:    "normalize example 1",
			weights: []float32{200, 100, 0.1},
		},
		{
			name:    "normalize example 2",
			weights: []float32{300.22, 0.7, 17, 54},
		},
		{
			name:    "normalize example 3",
			weights: []float32{300, 0.02, 17},
		},
		{
			name:    "normalize example 4",
			weights: []float32{500, 0.02, 17.4, 180},
		},
		{
			name:    "normalize example 5",
			weights: []float32{500, 0.02, 17.4, 2, 4, 5, .88},
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := moduletools.NormalizeWeights(tt.weights); !checkNormalization(got) {
				t.Errorf("Vectorizer.normalizeWeights() = %v, want %v", got, 1.0)
			}
		})
	}
}

func checkNormalization(weights []float32) bool {
	var result float32
	for i := range weights {
		result += weights[i]
	}
	return result == 1.0
}
